﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace RIFF
{
    public class Container
    {
        protected const Int32 RiffString = 0x46464952;      // "RIFF"

        public List<Chunk> Chunks = new List<Chunk>();
        public Int32 FormatTag;

        public void Write(Stream target)
        {
            MemoryStream buffer = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(target);

            foreach (Chunk c in Chunks)
            {
                c.Write(buffer);
            }

            // add 4 bytes for the format tag
            Int32 length = (Int32)buffer.Length + 4;

            writer.Write(RiffString);
            writer.Write(length);
            writer.Write(FormatTag);
            writer.Write(buffer.ToArray());
            writer.Flush();
            buffer.Dispose();
        }
    }
}
